<?php
/**
 *Template Class
 *
 *Class to separate the HTML of the PHP code
 *
 *This class take a template and change the vars for it value
 * 
 *@author Alberto Berroteran <ashrey10@yahoo.es>
 *@version 0.1 Beta
 *@copyright Copyrigth (c)2008 Alberto Berroteran
 */

class ABView{
	protected static $s_content = '';
	protected static $s_dir = '';

	public $onFinish = null;
	public $i_expire  = 0; #cache expiration
	protected $gziped = false;
	protected $level = 0;
	protected $a_tvars = array();
	protected $s_view;
	protected $s_tpl;
	protected $s_path;

	function __construct($s_dir, $s_view, $s_tpl='index', $cb_func = null){
		$this->s_path = PATH_TPL . $s_dir .'/';
		self::$s_dir = $this->s_path;
		$this->s_view = $s_view;
		$this->s_tpl = $s_tpl;
		$this->onFinish = $cb_func;
	}
	
	function changeRender($s_view, $s_tpl = '', $s_dir =''){
		$this->s_view = $s_view;
		$this->s_tpl = empty($s_tpl)?$this->s_tpl:$s_tpl;
		$this->s_path = empty($s_dir)?$this->s_path:PATH_TPL .$s_dir;
		self::$s_dir = $this->s_path;
	}

	function setInWhite($a_var){
		if(is_array($a_var)){
			foreach($a_var as $s_value){
				$this->a_tvars[$s_value] = '';
			}
		} else throw new ABException('Expected Array');
	}

	function __get($s_key){
		return isset($this->a_tvars[$s_key])?$this->a_tvars[$s_key]:null;
	}

	public function __set($s_name, $m_value) {
		$this->set($s_name, $m_value);
	}
	
	public function set($s_name, $m_value){
		if($s_name == 'multi' && is_array($m_value)){
			foreach($m_value as $s_key=>$s_value){
				$this->a_tvars[$s_key] = $s_value;
			}
		}else{
			$this->a_tvars[$s_name] = $m_value;
		}
	}
	/*
	 * This function parse the template and change for it value
	 */
	function render($b_only = false,$gziplevel = 0){
		ob_start(array($this, 'finish'));
		if($gziplevel > 0){
			$Encoder = ABApi::CheckCanGzip();
			if($Encoder and function_exists('gzcompress')){
				header('Content-Encoding: ' . $Encoder);
				$this->level = $gziplevel;
				$this->gziped = true;
			}
		}
		if($this->i_expire > 0){
			header('Expires: ' .date ('D, j M Y H:i:s \G\T\M', time() + $this->i_expire));
		}
		//Render the view
		//var_dump($this->a_tvars);
		ob_start();
		if(!is_dir($this->s_path))die("The template <strong>{$this->s_path}</strong> not exists");
		$s_file = "{$this->s_path}view/{$this->s_view}.phtml";
		if(!is_file($s_file))
			throw new ABException("The view <strong>$s_file</strong> not found!");
		extract($this->a_tvars, EXTR_PREFIX_ALL, 'tpl');
		include($s_file);
		self::$s_content = ob_get_clean();
		
		//render the Template if not use view only
		$s_file = "{$this->s_path}{$this->s_tpl}.phtml";
		if(!$b_only and !empty($s_file)){
			if(is_file($s_file)){
				include $s_file;
			}else die('Template not exist! '.$s_file);
		}else{
			echo self::$s_content;
		}
	}
	
	protected function finish($s_output){
		if($this->onFinish != null and is_callable($this->onFinish)){
			$temp = call_user_func($this->onFinish, $s_output);
		}else{
			$temp = $s_output;
		}
		return ($this->gziped)? ABView::gziper($temp, $this->level) : $temp; 
	}
	
	function __destruct(){
		$i_control = 0;
		while(ob_get_level() > 0){
			$i_control++;
			if($i_control > 10) break;
			ob_end_flush();
		}
	}
	
	/**
	 * Here start the static method
	 */
	public static function content(){
		echo self::$s_content;
	}
	
	public static function partial($s_name, $a_args = array()){
		$s_file = self::$s_dir."partial/$s_name.phtml";
		$s_kfile = PATH_ABS."/kernel/partial/$s_name.phtml";
		extract($a_args, EXTR_PREFIX_ALL, 'tpl');
		if(is_file($s_file)){
			include($s_file);
		}elseif(is_file($s_kfile)){
			include($s_kfile);
		}else throw new ABException("The partial <strong>$s_name</strong> not found!");
	}
}//end of class
?>
